package com.browan.background;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Util {

	private static HttpClient client = new HttpClient();

	public static List<BackgroundData> getImages(String url) {

		List<BackgroundData> datas = new ArrayList<BackgroundData>();

		try {
			String source = client.getMethod(url, "utf-8");

			String regex = "<div *class=\"center *pic_list\">(.*?)</ *div>";

			Pattern pattern = Pattern.compile(regex);

			Matcher matcher = pattern.matcher(source);

			if (matcher.find()) {

				String imageSource = matcher.group(1).trim();

				String images[] = imageSource.split("</a>");

				for (String image : images) {

					pattern = Pattern.compile("src *= *\"(.*?)\"");

					matcher = pattern.matcher(image);

					BackgroundData data = new BackgroundData();

					if (matcher.find()) {
						data.little_url = matcher.group(1);
					}

					pattern = Pattern.compile("imgurl *= *(.*?)\"");
					matcher = pattern.matcher(image);
					if (matcher.find()) {
						data.big_url = matcher.group(1);
					}
					if (data.isAdd()) {
						datas.add(data);
					}
				}
			}

		} catch (Exception e) {
			e.printStackTrace();
		}

		return datas;
	}
}